package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lixu
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    private DtsRoleMapper dtsRoleMapper;

    /**
     * 根据角色IDS查询 角色名称集合
     *
     * @param roleIds
     * @return
     */
    @Override
    public Set<String> findDtsRoleNameByIds(Integer[] roleIds) {
        List<DtsRole> dtsRoleList = dtsRoleMapper.selectBatchIds(Arrays.asList(roleIds));
        //Stream流   可读性极差
        Set<String> roles = dtsRoleList.stream().map(DtsRole::getName).collect(Collectors.toSet());
        return roles;
    }

    /**
     * 查询角色选项
     *
     * @return
     */
    @Override
    public List<CatVo> findCatVoList() {
        List<DtsRole> dtsRoleList = dtsRoleMapper.selectList(new QueryWrapper<DtsRole>()
                .select("id,name").eq("deleted", 0));
        return dtsRoleList.stream().map(dtsRole -> {
            CatVo vo = new CatVo();
            vo.setValue(dtsRole.getId());
            vo.setLabel(dtsRole.getName());
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 查询角色分页
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param rolename
     * @return
     */
    @Override
    public IPage<DtsRole> findPage(Integer page, Integer limit, String sort, String order, String rolename) {
        return dtsRoleMapper.selectPage(new Page<DtsRole>(page, limit), new QueryWrapper<DtsRole>()
                .eq(StringUtils.isEmpty(rolename) ? false : true, "name", rolename)
                .eq("deleted", 0)
                .orderBy(StringUtils.isEmpty(sort) ? false : true, "asc".equalsIgnoreCase(order) ? true : false, sort));
    }
}
